package com.wuzhiming.istudy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuzhiming.istudy.pojo.Course;
import com.wuzhiming.istudy.pojo.Note;
import com.wuzhiming.istudy.utils.ImageUtils;
import com.wuzhiming.istudy.utils.LogUtils;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;

/**
 * @ClassName: ImageController
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/7/13 15:41
 */

@RestController
@RequestMapping("/image")
public class ImageController extends BaseController{
    private Logger logger = LogUtils.getInstance(ImageController.class);
    
    /**
    * @Description: 图片上传
    * @Param: [file, flag, id]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/13
    */
    @PostMapping("/upload")
    public String upload(@RequestParam("image") MultipartFile file,@RequestParam("flag") Integer flag,Integer id) throws IOException {
        //判断是否超过大小
        if(!ImageUtils.checkFileSize(file.getSize(),1,"M")){
            result.setCode(502);
            return JSONObject.toJSONString(result);
        }
        //上传图片，并且返回图片存放的路径
        String imgPath = ImageUtils.upload(file,"o");
        if(flag!=null || flag!=5){
            if(flag<=4){
                updateAdmin(flag,imgPath);
            }else if(flag==6){
                updateImg(flag,imgPath,id);
            }
        }
        // 返回结果
        result.setCode(200);
        result.setData(imgPath);
        return JSONObject.toJSONString(result);
    }

    /**
     * @Description: 更新管理员的图片信息
     * @Param: [flag, imgPath]
     * @return: void
     * @Author: zhimingw
     * @Date: 2021/7/13
     */
    public void updateAdmin(Integer flag,String imgPath){
        // 记录原来图片的位置
        String img = "";
        // 更新管理员信息
        if(flag<=3){
            if (flag == 1){
                img = adminInfo.getImg();
                adminInfo.setImg(imgPath);
                logger.warn("更新管理员头像，flag:"+flag+"，imgPath:"+imgPath);
            }else if (flag == 2){
                img = adminInfo.getWechat();
                adminInfo.setWechat(imgPath);
                logger.warn("更新管理员微信图片，flag:"+flag+"，imgPath:"+imgPath);
            }else if (flag == 3){
                img = adminInfo.getPublicWechat();
                adminInfo.setPublicWechat(imgPath);
                logger.warn("更新管理员公众号图片，flag:"+flag+"，imgPath:"+imgPath);
            }
            if (userService.update(adminInfo)) {
                // 更新session
                session.setAttribute("admin",adminInfo);
                logger.warn("【成功】更新管理员图片信息");
            }else {
                logger.warn("【失败】更新管理员图片信息");
            }
        }else if(flag==4){
            img = userInfo.getImg();
            userInfo.setImg(imgPath);
            logger.warn("更新用户头像，flag:"+flag+"，imgPath:"+imgPath);
            if (userService.update(userInfo)) {
                // 更新session
                session.setAttribute("user",userInfo);
                logger.warn("【成功】更新用户头像信息");
            }else {
                logger.warn("【失败】更新用户头像信息");
            }
        }
    }

    /**
     * 更新图片信息（课程封面）
     */
    public void updateImg(Integer flag,String imgPath,Integer id){
        // 记录原来图片的位置
        String img = "";
        if (flag == 6){   // 更新课程封面
            Course course = courseService.queryById(id);
            course.setImgUrl(imgPath);
            if (courseService.update(course)) {
                logger.warn("【成功】更新课程封面，flag:"+flag+"，imgPath:"+imgPath+"，id:"+id);
            }else {
                logger.warn("【失败】更新课程封面，flag:"+flag+"，imgPath:"+imgPath+"，id:"+id);
            }
        }
    }
}
